#include "../../include/ManageCompare.h"

using namespace std;

CompareController* compareController;
CarController* smartCarController;
StudentController* studentController;

void manageCompare(CompareController* _compareController,StudentController* _stuController,CarController* _carController) {
    compareController = _compareController;
    smartCarController = _carController;
    studentController = _stuController;
    string cmd;     // 输入命令
    while (cmd != E_X_I_T ) {
        cmd = "";
        cout << "=========主界面==========" << endl;
        cout << "指令说明:" << endl;
        cout << "0. autoCompare   自动匹配" << endl;
        cout << "1. createCompare 创建匹配" << endl;
        cout << "2. listCompare   列出所有匹配ID" << endl;
        cout << "3. deleteCompare 删除匹配" << endl;
        cout << "4. storeData     保存匹配数据" << endl;
        cout << "5. loadData      加载匹配数据" << endl;
        cin >> cmd;
        if(cmd == "0" || cmd == AUTO_COM) {
            autoCompare();
        }
        else if(cmd == "1" || cmd == CREATE_COM) {
            createCompare();
        }
        else if(cmd == "2" || cmd == LIST_COM) {
            listCompare();
        }
        else if(cmd == "3" || cmd == DELETE_COM) {
            deleteCompare();
        }
        else if(cmd == "4" || cmd == STORE_DATA) {
            storeCompareData();
        }
        else if(cmd == "5" || cmd == LOAD_DATA) {
            loadCompareData();
        }
        cout << "=======================" << endl;
    }
    cout << "已退出程序。" << endl;
    return;
}
void autoCompare() {
    int lengthStudentList = studentController->getStudentCount();
    Student* studentList[lengthStudentList];
    studentController->listAllStudent(studentList);
    int lengthCarList = smartCarController->getCarCount();
    SmartCar* carList[lengthCarList];
    smartCarController->listAllCar(carList);
    int groupSize = lengthStudentList/lengthCarList;
    int leftStudentCount = lengthStudentList%lengthCarList;
    if(leftStudentCount != 0)
        cout << leftStudentCount << "个组为 " << groupSize+1 << "人" << endl;
    cout << lengthCarList-leftStudentCount << "个组为 " << groupSize << "人" << endl;
    for(int i=0;i<lengthCarList;i++) {
        for(int j=0;j<groupSize;j++) {
            compareController->addCompare(studentList[groupSize*i+j],carList[i]);
        }            
    }
    for(int i=0;i<leftStudentCount;i++) {
        compareController->addCompare(studentList[groupSize*lengthCarList+i],carList[i]);
    }
    cout << "自动分配完成!" << endl;
}
void createCompare() {
    cout << "========创建匹配界面========" << endl;
    Student* student = getStudentByNum();
    SmartCar* car = getCarByNum();
    compareController->addCompare(student,car);
    cout << "已将" << student->toString() << "与" << car->getId() << "匹配!" << endl;
}
void listCompare() {
    int length = compareController->getCompareCount();
    Compare* compareList[length];
    compareController->listAllCompare(compareList);
    cout << "共有" << length << "项匹配:" << endl;
    for(int i=0;i<length;i++) {
        cout <<  i+1 << " : " << compareList[i]->getId() << " | " << compareList[i]->getStuId() << " - " << compareList[i]->getCarId() << endl;
    }
    cout << endl;
}
void deleteCompare() {
    int length = compareController->getCompareCount();
    Compare* compareList[length];
    compareController->listAllCompare(compareList);
    int compareNum;
    while(true) {
        cout << "共有" << length << "项匹配:" << endl;
        for(int i=0;i<length;i++) {
            cout <<  i+1 << " : " << compareList[i]->getId() << " | " << compareList[i]->getStuId() << " - " << compareList[i]->getCarId() << endl;
        }
        cout << endl;
        cout << "请输入要删除的匹配序号:" << endl;

        cin >> compareNum;
        compareNum -= 1;
        if(compareNum >= 0 && compareNum < length) {
            break;
        }
        cout << "输入序号有误!" << endl;
    }
    compareController->deleteCompare(compareList[compareNum]);
    cout << "删除成功!" << endl;
}
void storeCompareData() {
    ofstream fout(CompareDataPath);
    int length = compareController->getCompareCount();
    Compare* compareList[length];
    compareController->listAllCompare(compareList);
    for(int i=0;i<length;i++) {
        fout << compareList[i]->toString() << endl;
    }
}
void loadCompareData() {
    if(compareController->getCompareCount()!=0) {
        cout << "当前小车数量不为0,为避免冲突,请勿直接导入数据。" << endl;
        return;
    }
    ifstream fin(CompareDataPath);
    std::string line;
    while(std::getline(fin,line)) {
        std::istringstream iss(line);
        compareController->strToCompare(iss);
    }
}